package com.wanbangee.marksystem.service.imp;


import java.text.DateFormat;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wanbangee.marksystem.dao.SScoreDao;
import com.wanbangee.marksystem.dao.SScoreDetailDao;
import com.wanbangee.marksystem.entities.MAnswerSheetRecord;
import com.wanbangee.marksystem.entities.SScore;
import com.wanbangee.marksystem.service.SubmitTestService;
import com.wanbangee.marksystem.service.score.ExamInfoService;
import com.wanbangee.marksystem.service.score.SpecialTestService;

/**
 * 提交试题（业务层）
 * @author 小粥愚
 *
 */
@Service
public class SubmitTestServiceImpl implements SubmitTestService {
	@Autowired
	private SScoreDao scoreDao ;
	@Autowired
	private SScoreDetailDao scoreDetailDao;
	@Autowired
	private SpecialTestService specialTestService = null;
	@Autowired
	private ExamInfoService examInfoService = null;
	
	/**
	 * 正常卷的提交分数（正常提交）	(测试结果：✔)
	 * @param examId
	 * @param examSubId
	 * @param teaId
	 * @param topicNo
	 * @param score
	 * @param scoreId
	 * @param recordId 试卷的记录类型（0：正常卷，2：优秀卷，3：典型错误卷）
	 * @return
	 */
	@Transactional
	@Override
	public Boolean submitNomalTestScore(Integer examId, Integer examSubId, Integer teaId,String teaNm, String topicNo, Double score,
			Integer scoreId,Long recordId,Integer markerId) {
		Integer num = 0;
		Boolean flag = false;
		
		//判断是否是优秀卷/典型错误卷
		if(recordId == 0) {
			//如果不是，只提交分数
			String scoreName = this.scoreDao.selectScoreNameByExamIdExamSubIdTeaIdScoreId(examId, examSubId, teaId, scoreId);
			num = this.scoreDetailDao.updateScoreByExamIdExamSubIdScoreIdScoreScoreName(examId, examSubId, scoreId, score, scoreName);
			
		}else {
			//如果是，提交分数并将该题加入答案记录表中
			//1. 提交分数
			String scoreName = this.scoreDao.selectScoreNameByExamIdExamSubIdTeaIdScoreId(examId, examSubId, teaId, scoreId);
			num = this.scoreDetailDao.updateScoreByExamIdExamSubIdScoreIdScoreScoreName(examId, examSubId, scoreId, score, scoreName);
			
			//2. 记录特殊卷信息、试卷类型
			MAnswerSheetRecord sheet = this.specialTestService.specialTestInfo(examId, examSubId, markerId, scoreId,teaId,teaNm);
			System.out.println("score+submitNomalTestScore:"+sheet+"------------");
			this.specialTestService.addSpecialTest(sheet, recordId);
		}
		if(num!=0) {
			flag = true;
		}
		
		return flag;
	}


	/**
	 * 问题卷提交
	 * @param examId
	 * @param examSubId
	 * @param teaId
	 * @param topicNo
	 * @param score
	 * @param scoreId
	 * @param recordId	试卷的记录类型（0：正常卷，2：优秀卷，3：典型错误卷）
	 * @param markerId
	 * @param exceptionInfo	问题卷信息（1 图像不完整,2 图像不清晰 ,3 字迹不清晰,4 其他）
	 * @param exceptionRemark 问题卷描述
	 * @return
	 */
	@Transactional
	@Override
	public Boolean submitProblemTestScore(Integer examId, Integer examSubId, Integer teaId, String teaNm,String topicNo,
			Double score, Integer scoreId, Long recordId, Integer markerId, Long exceptionInfo,
			String exceptionRemark) {
		//1、查询问题卷基本信息
		MAnswerSheetRecord sheet = this.specialTestService.specialTestInfo(examId, examSubId, markerId, scoreId, teaId, teaNm);
		sheet.setExceptionInfo(exceptionInfo);//异常信息
		sheet.setExceptionRemark(exceptionRemark);//备注
		
		//2、插入记录特殊卷的表中（答题卡记录表：m_answer_sheet_record）
		this.specialTestService.addSpecialTest(sheet, recordId);
		
		//3、删除阅卷打分表中的该条数据
		this.scoreDao.delectScoreByExamIdExamSubIdScoreId(examId, examSubId, scoreId);
		
		//4、完成		
		
		return true;
	}
	
	
	/**
	 * 仲裁卷的提交分数（正常提交）
	 * @param examId
	 * @param examSubId
	 * @param teaId
	 * @param teaNm
	 * @param topicNo
	 * @param score
	 * @param scoreId
	 * @param recordId 试卷的记录类型（0：正常卷，2：优秀卷，3：典型错误卷）
	 * @param markerId
	 * @return
	 */
	@Transactional
	@Override
	public void submitNomalArbitrationTestScore(Integer examId, Integer examSubId, Integer teaId, String teaNm,
			String topicNo, Double score, Integer scoreId, Long recordId, Integer markerId) {
		//判断是否是优秀卷/典型错误卷
		if(recordId == 0) {
			//如果不是，只提交分数
			this.scoreDetailDao.updateArbitrationScoreByExamIdExamSubIdScoreIdScoreScoreName(examId, examSubId, scoreId, score);
			
		}else {
			//如果是，提交分数并将该题加入答案记录表中
			//1. 提交分数
			this.scoreDetailDao.updateArbitrationScoreByExamIdExamSubIdScoreIdScoreScoreName(examId, examSubId, scoreId, score);
			
			//2. 记录特殊卷信息、试卷类型
			MAnswerSheetRecord sheet = this.specialTestService.specialTestInfo(examId, examSubId, markerId, scoreId,teaId,teaNm);
			this.specialTestService.addSpecialTest(sheet, recordId);
		}
	}
	/**
	 * 问题卷的提交分数（正常提交）
	 * @param examId
	 * @param examSubId
	 * @param teaId
	 * @param score   得分
	 * @param recordId	记录表id
	 * @return
	 */
	@Transactional
	@Override
	public void submiProblemTestScore(Integer examId, Integer examSubId, Integer teaId,Double score, Integer recordId) {
		//1、查询问题卷信息
		String addTime = DateFormat.getDateTimeInstance().format(new Date());
		Map<String, Object> map = this.examInfoService.examProblemTestInfo(recordId);
		//将feign接口传过来的map对象里面的object对象值，转成实体对象
		ObjectMapper mapper = new ObjectMapper(); 
		MAnswerSheetRecord examPromTestInfo = mapper.convertValue(map.get("examPromTestInfo"), MAnswerSheetRecord.class); 
		
//		MAnswerSheetRecord examPromTestInfo = (MAnswerSheetRecord) map.get("examPromTestInfo");
		SScore scoreT = new SScore();
		scoreT.setStuId(Integer.valueOf(examPromTestInfo.getStuId().toString()));
		scoreT.setStuNm(examPromTestInfo.getStuNm());
		scoreT.setTopicNo(examPromTestInfo.getTopicNo());
		scoreT.setTopicScore(examPromTestInfo.getTopicScore());
		scoreT.setTopicType(2);
		scoreT.setTopicUrl(examPromTestInfo.getTopicUrl());
		scoreT.setScoreType(1);
		scoreT.setTeaId1(teaId);
		scoreT.setMarkerId(Integer.valueOf(examPromTestInfo.getMarkerId().toString()));
		scoreT.setAddTime(addTime);
		scoreT.setAddUserId(teaId);
		
		
		//2、将问题卷信息和得分插入到打分表和打分明细表中
		//	- 插入打分表，返回主键
		this.scoreDao.insertScore(scoreT, examId, examSubId);
		
		System.out.println("id===="+scoreT.getScoreId());
		//	- 插入打分详细表
		this.scoreDetailDao.insertScoreDetail(examId, examSubId, scoreT.getScoreId(), score);
		
		//3、修改问题卷类型（改为已处理问题卷）
		this.examInfoService.updateProblemTestInfo(recordId);
		
		
	}
	
	
	/**
	 * 重评卷的提交分数（正常提交）
	 * @param examId
	 * @param examSubId
	 * @param teaId
	 * @param topicNo
	 * @param score
	 * @param scoreId
	 * @param recordId 试卷的记录类型（0：正常卷，2：优秀卷，3：典型错误卷）
	 * @return
	 */
	@Override
	public Boolean submitRepeatTestScore(Integer examId, Integer examSubId, Integer teaId,String teaNm, String topicNo, Double score,
			Integer scoreId,Long recordId,Integer markerId) {
		Integer num = 0;
		Boolean flag = false;
		
		//判断是否是优秀卷/典型错误卷
		if(recordId == 0) {
			//如果不是，只提交分数
			String scoreName = this.scoreDao.selectScoreNameByExamIdExamSubIdTeaIdScoreId(examId, examSubId, teaId, scoreId);
			num = this.scoreDetailDao.updateScoreByExamIdExamSubIdScoreIdScoreScoreName(examId, examSubId, scoreId, score, scoreName);
			//查询阅卷类型
			Integer scoreType = scoreDao.selectSScoreByExamIdExamSubIdMarkerIdTopicNo(examId, examSubId, markerId, Integer.valueOf(topicNo));
			//并且修改重评状态
			scoreDao.updateSScoreByExamIdExamSubIdMarkerIdTopicNo(examId, examSubId, markerId, Integer.valueOf(topicNo),scoreType);
			
		}else {
			//如果是，提交分数并将该题加入答案记录表中
			//1. 提交分数
			String scoreName = this.scoreDao.selectScoreNameByExamIdExamSubIdTeaIdScoreId(examId, examSubId, teaId, scoreId);
			num = this.scoreDetailDao.updateScoreByExamIdExamSubIdScoreIdScoreScoreName(examId, examSubId, scoreId, score, scoreName);
			
			//2. 记录特殊卷信息、试卷类型
			MAnswerSheetRecord sheet = this.specialTestService.specialTestInfo(examId, examSubId, markerId, scoreId,teaId,teaNm);
			this.specialTestService.addSpecialTest(sheet, recordId);
		}
		if(num!=0) {
			flag = true;
		}
		
		return flag;
	}

}
